﻿var Message =
{
  replyId: -1,
  parentMessage: '',
  init: function() {

  },

  create: function(url) {
    var msg = $('#message').val();

    $("#createMessage").text("Sending message...").show();

    $.ajax({
      type: "POST",
      dataType: "json",
      url: url,
      data: { message: msg },
      success: function(result) {
        $('#message').val("");
        $("#createMessage").hide("slow");
        window.location.reload();
      },
      error: function(result) {
        $("#createMessage").text(result.errorMessage).show();
      }
    });
  },

  deletemessage: function(url, msgid) {
    if (confirm("Are you sure you want to delete this message?")) {
      $.ajax({
        type: "POST",
        dataType: "json",
        url: url,
        data: { messageId: msgid },
        success: function(result) {
          if (result.isSuccessful) {
            $("#msgview-" + msgid).addClass("hilite").fadeOut(1000);
          } else {
            alert(result.errorMessage);
          }
        },
        error: function(result, status) {
          alert(result.errorMessage);
        }
      });
    }
  },

  showReply: function(id) {
    Message.replyId = id;
    $("#parentMessage #msgHtml").html(
                            $("#msgview-" + id + " #msgHtml").html());
    $("#divReplyBox").modal();
    $("#replyMessage").focus();
  },

  hideReply: function() {
    Message.replyId = -1;
    Message.parentMessage = "";
    $.modal.close();
  },

  reply: function() {
    var url = "/message/reply"
    var msg = $("#replyMessage").val();
    var msgId = Message.replyId;

    $("#replyStatus").text("Sending reply...").show();

    $.ajax({
      type: "POST",
      dataType: "json",
      url: url,
      data: { messageid: msgId,
        Message: msg
      },
      success: function(result) {
        if (result.isSuccessful) {
          $("#replyMessage").val("");
          window.location.reload();
        } else {
          $("#replyStatus").text(result.errorMessage).show();
        }
      },
      error: function(result) {
        $("#replyStatus").text("Unexpected error").show();
      }
    });
  },

  reload: function() {

    var username = $("#currentUsername").val();
    $.ajax({
      type: "POST",
      dataType: "html",
      url: "/message/RefreshMessages",
      data: { username: username
      },
      success: function(data) {
        $("#recentmessages").html(data);
        //        $("#divFileProgressContainer").empty();
        //        $("#thumbnails").empty();
      },
      error: function(result) {
        alert("Error: " + result);
      }
    });
  },

  showImage: function(messageId, url) {
    var divImage = $("#imgViewer");
    var img = $("#imgViewer img");
    var lnk = $("#imgViewer a")
    lnk.attr("href", url);
    img.attr("src", url);
    img.attr("width", 420);
    divImage.modal({ overlay: 50 });
  }

}